package com.lcq.study.javabase;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TypeConvertTest
 * @Description TODO
 * @Author lichuanqi
 * @Date 2021/4/20 11:42
 * @Version 1.0
 **/
public class TypeConvertTest {
    public static void main(String[] args) {

    }

    public static void convertTest() {
        Person p1 = new Student();
        Person p2 = new Teacher();
        Teacher student = convert(p1, Teacher.class);
        Teacher teacher = convert(p2, Teacher.class);
        //System.out.println(student.haha());
        System.out.println(teacher.hehe());
        List<Student> students = convertList(p1, Student.class);
        System.out.println(students);
    }

    public static <T> T convert(Person p, Class<T> zz) {
        return (T) p;
    }

    public static <T> List<T> convertList(Person p, Class<T> zz) {
        ArrayList<T> objects = new ArrayList<>();
        objects.add((T) p);
        return objects;
    }
}

interface Person {
    String getName();
}

class Student implements Person {

    @Override
    public String getName() {
        return "Student";
    }

    public String haha() {
        return "haha";
    }
}

class Teacher implements Person {

    @Override
    public String getName() {
        return "Teacher";
    }

    public String hehe() {
        return "hehe";
    }
}
